import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import HeaderAsideLayout from '@/layouts/HeaderAsideLayout/Layout.vue'
import { getTokens } from '@/utils/cookie'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'dashboard',
    component: HeaderAsideLayout,
    redirect: '/dashboard/analysis',
    children: [
      {
        path: 'dashboard/analysis',
        name: 'dashboard.analysis',
        meta: {
          title: '分析页'
        },
        component: () => import('@/views/dashboard/analysis/index.vue')
      },
      {
        path: 'dashboard/monitor',
        name: 'dashboard.monitor',
        meta: {
          title: '监控页'
        },
        component: () => import('@/views/dashboard/monitor/index.vue')
      },
      {
        path: 'dashboard/workplace',
        name: 'dashboard.workplace',
        meta: {
          title: '控制台'
        },
        component: () => import('@/views/dashboard/workplace/index.vue')
      }
    ]
  },
  {
    path: '/user',
    name: 'user',
    component: HeaderAsideLayout,
    redirect: '/user/list',
    children: [
      {
        path: 'list',
        name: 'user.list',
        meta: {
          title: '用户管理'
        },
        component: () => import('@/views/user/userList.vue')
      },
      {
        path: 'group/list',
        name: 'user.group',
        meta: {
          title: '分组管理'
        },
        component: () => import('@/views/user/groupList.vue')
      }
    ]
  },
  {
    path: '/user/login',
    name: 'user.login',
    component: () => import('@/views/user/login.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(process.env.VUE_APP_PUBLIC_PATH),
  routes
})

router.beforeEach((to, from, next) => {
  if (!getTokens() && to.name !== 'user.login') next({ name: 'user.login' })
  else next()
})

export default router
